package com.yao.mylock.controller;

import com.yao.mylock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Macrydy
 * @date 2022/9/5
 *
 */
@RestController
public class StockController {

    //演示案例，不连接数据库，使用传统锁：ReentrantLock、synchronized
    //@Autowired
    //private StockService1 stockService;

    @Autowired
    private StockService stockService;


    //mybatisplus 减库存
    @GetMapping("stock/deduct")
    public String deduct() {
        //使用JVM锁：ReentrantLock、synchronized
        //stockService.deduct();

        //使用mysql悲观锁  select .. for update
        //stockService.deduct2();

        //使用mysql乐观锁
        //stockService.deduct3();

        //使用redis
        //stockService.deduct4();

        //使用redis分布式锁
        //stockService.deduct5();

        //使用Redisson框架实现分布式锁（自带可重入和自动续期）
        //stockService.deduct6();

        //使用curator框架实现分布式锁
        //stockService.deduct7();

        //使用mysql实现分布式锁
        stockService.deduct8();

        return new String("减库存成功！");
    }
}
